<template>
	<view class="p-20">

		<u-cell-group :border="false" class="u-cell-group-all" v-if="ifSetPassword">
			<u-cell size="large" title="修改安全密码" :isLink="true" @click="toPage('modifyPassword')">
			</u-cell>
			<u-cell size="large" title="忘记安全密码" :isLink="true" @click="toPage('forgetPassword')">
			</u-cell>
		</u-cell-group>

		<view v-else>
			<set-input placeholder="请输入6位数字安全密码" @input="changePassword" class="m-b-15"></set-input>
			<set-input placeholder="再次输入密码" @input="changePasswordAgin" class="m-b-30"></set-input>
			<u-button shape="circle" color="#000000" size="large" @click="confirm">确认</u-button>
		</view>

	</view>
</template>

<script>
	const permissionUtils = require('@/common/permission.js')
	import setInput from './common/setInput.vue'
	import {
		face
	} from '../../utility/face.js'
	import {userIfSetPasswordApi,userFaceImgUploadApi,setPasswordApi,ifCanBeModifyApi} from '../../api/collection.js'
	import { getQiniuData,uploadQiniu } from '../../utility/upload.js'
	import md5 from 'js-md5';
	export default {
		components: {
			setInput
		},
		data() {
			return {
				password: '',
				newPassword: '',
				ifSetPassword:false,
			}
		},
		onLoad() {
			this.getIfPassword()
		},
		methods: {

			//获取是否设置安全密码
			async getIfPassword(){
				try{
					uni.showLoading({
						title:'请稍后',
						mask:true
					})
					const res = await userIfSetPasswordApi()
					this.ifSetPassword = res.data.isHasPhoto
					!this.ifSetPassword ? this.faceDistinguish() : ''
					uni.hideLoading()
				}catch(e){
					//TODO handle the exception
					uni.hideLoading()
				}
			},

			changePassword(val) {
				this.password = val
			},
			changePasswordAgin(val) {
				this.newPassword = val
			},
			confirm() {
				switch (true) {
					case !this.password || !this.newPassword:
						uni.showToast({
							title: '安全密码或确认密码不能为空',
							icon: 'none'
						})
						break;
					case !uni.$u.test.number(this.password) || this.password.length !== 6:
						uni.showToast({
							title: '请输入6位数字安全密码',
							icon: 'none'
						})
						break;
					case this.password !== this.newPassword:
						uni.showToast({
							title: '两次输入密码不一致',
							icon: 'none'
						})
						break;
					default:
					    console.log('验证码成功')
						this.setPassword()
						break;
				}

			},
			async toPage(name) {
				try{
					if(name === 'modifyPassword'){
						const res = await ifCanBeModifyApi()
					}
					uni.navigateTo({
						url: `./${name}`
					})
				}catch(e){
					//TODO handle the exception
					uni.showToast({
						title: e.message || e.data,
						icon: 'none'
					})
				}

			},

			//设置密码
			async setPassword(){
				try{
					uni.showLoading({
						title:'请稍后',
						mask:true
					})
					const res = await setPasswordApi({
						code: md5(this.$store.state.basePassword + this.password),
						repeat:md5(this.$store.state.basePassword + this.newPassword),
						type:1
					})
					uni.hideLoading()
					uni.showToast({
						title: '设置成功',
						icon: 'none'
					})
					setTimeout(()=> {
						uni.navigateBack({
							delta:2
						})
					}, 800);
				}catch(e){
					uni.hideLoading()
					uni.showToast({
						title: e.message || e.data,
						icon: 'none'
					})
				}
			},

			//人脸识别
			async faceDistinguish() {
				try {
					//人脸调用
					const res = await face()
					uni.showLoading({
						title:'正在验证人脸',
						mask:true
					})
					//七牛token
					const qiniuRes = await getQiniuData()
					//七牛上传
					const url = await uploadQiniu(qiniuRes.data,{path:res,name: parseInt(Math.random() * (999999 - 100000 + 1) + 100000, 10)})
					//后台上传
					const uploadRes = await userFaceImgUploadApi({
						photoUrl:qiniuRes.data.domain + url.key
					})
					this.ifSetPassword = false
					uni.hideLoading()
					uni.showToast({
						title:'人脸校验通过',
						icon:'none'
					})
				} catch (e) {
					console.log(e)
					uni.hideLoading()
					uni.showToast({
						title: e.message || e.data,
						icon: 'none'
					})
					if (e.code === -5 && plus.os.name === 'Android') {
						this.showModal({
							title: '权限受限',
							msg: '您拒绝了一些权限，可能导致该服务无法正常使用，请前往设置！',
							confirmText: '前往设置',
						}).then(() => {
							permissionUtils.gotoAppPermissionSetting()
							uni.navigateBack()
						}).catch((err) =>{
							uni.navigateBack()
						})
					}else{
						setTimeout(()=> {
							uni.navigateBack()
						}, 800);
					}
				}
			},

			/**
			 * 显示弹窗提示
			 */
			showModal({
				title,
				msg,
				confirmText
			}) {
				return new Promise((resolve, reject) => {
					uni.showModal({
						title,
						content: msg,
						showCancel: true,
						confirmText: confirmText || '确认',
						success(res) {
							if (res.confirm) {
								resolve()
							} else {
								reject()
							}
						},
						fail() {
							reject()
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-cell-group-all {
		font-size: 28rpx;

		.u-slot-value-sign {
			font-size: 24rpx;
			color: #B3B4B5;
			display: flex;
			align-items: center;
		}

		::v-deep .u-cell__body--large {
			padding: 40rpx 0;
			.u-cell__title-text--large[data-v-1c4434ae] {
			    font-size: 14px;
				font-weight: bold;
			}
		}
	}
</style>
